<template>
<div class="page-container">
    <!--工具栏-->
    <div class="toolbar">
        <el-form :inline="true" :model="filters" :size="size" ref="filters">
            <el-row class="search-input">
                <el-form-item prop="key" label="名称：">
                    <el-input v-model="filters.key" clearable placeholder="名称"></el-input>
                </el-form-item>
                <el-form-item prop="type" label="类型：">
                    <el-input v-model="filters.type" clearable placeholder="类型"></el-input>
                </el-form-item>
                <el-form-item prop="remark" label="备注：">
                    <el-input v-model="filters.remark" clearable placeholder="备注"></el-input>
                </el-form-item>
            </el-row>
            <el-row class="search-button">
                <el-form-item>
                    <kt-button icon="fa fa-search" :label="$t('action.search')" type="primary" @click="findPage(null)" />
                </el-form-item>
                <el-form-item>
                    <kt-button icon="fa fa-plus" :label="$t('action.add')" type="primary" @click="handleAdd" />
                </el-form-item>
                <el-form-item>
                    <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
                </el-form-item>
            </el-row>
            <el-form-item class="t_r w100">
                <el-button-group>
                    <el-tooltip content="刷新" placement="top">
                        <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
                    </el-tooltip>
                    <el-tooltip content="列显示" placement="top">
                        <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
                    </el-tooltip>
                    <!-- <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
            </el-tooltip>-->
                </el-button-group>
            </el-form-item>
            <!--表格显示列界面-->
            <table-column-filter-dialog ref="tableColumnFilterDialog" :columns="columns" @handleFilterColumns="handleFilterColumns"></table-column-filter-dialog>
        </el-form>
    </div>
    <!--表格内容栏-->
    <jic-table :height="350" :data="pageResult" :columns="filterColumns" @findPage="findPage" @handleEdit="handleEdit" @handleDelete="handleDelete" ref="JicTable"></jic-table>
    <!--新增编辑界面-->
    <el-dialog :title="operation?'新增':'编辑'" width="40%" :visible.sync="editDialogVisible" :close-on-click-modal="false">
        <el-form :model="dataForm" label-width="80px" :rules="dataFormRules" ref="dataForm" :size="size">
            <el-row class="searchDialog-input">
                <el-form-item label="ID：" prop="id" v-show="false">
                    <el-input v-model="dataForm.id" auto-complete="off" clearable placeholder="请输入ID"></el-input>
                </el-form-item>
                <el-form-item label="名称：" prop="key">
                    <el-input v-model="dataForm.key" auto-complete="off" clearable placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="值：" prop="val">
                    <el-input v-model="dataForm.val" auto-complete="off" clearable placeholder="请输入值"></el-input>
                </el-form-item>
                <el-form-item label="类型：" prop="type">
                    <el-input v-model="dataForm.type" auto-complete="off" clearable placeholder="请输入类型"></el-input>
                </el-form-item>
                <el-form-item label="备注：" prop="remark">
                    <el-input v-model="dataForm.remark" auto-complete="off" type="textarea" placeholder="请输入备注"></el-input>
                </el-form-item>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align:center">
            <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
            <el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">{{$t('action.submit')}}</el-button>
        </div>
    </el-dialog>
</div>
</template>

<script>
import JicTable from "@/views/Core/JicTable";
import KtButton from "@/views/Core/KtButton";
import {
    format
} from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
export default {
    components: {
        JicTable,
        KtButton,
        TableColumnFilterDialog
    },
    data() {
        return {
            size: "small",
            filters: {
                key: null,
                type: null,
                remark: null
            },
            columns: [{
                    prop: "id",
                    label: "ID",
                    minWidth: 50
                },
                {
                    prop: "key",
                    label: "名称",
                    minWidth: 100
                },
                {
                    prop: "val",
                    label: "值",
                    minWidth: 100
                },
                {
                    prop: "type",
                    label: "类型",
                    minWidth: 80
                },
                {
                    prop: "remark",
                    label: "备注",
                    minWidth: 120
                },
                {
                    prop: "createTime",
                    label: "创建时间",
                    minWidth: 120
                }
            ],
            filterColumns: [],
            pageRequest: {
                pageNum: 1,
                pageSize: 10
            },
            pageResult: {},

            operation: false, // true:新增, false:编辑
            editDialogVisible: false, // 新增编辑界面是否显示
            editLoading: false,
            dataFormRules: {
                key: [{
                    required: true,
                    message: "请输入名称",
                    trigger: "blur"
                }],
                val: [{
                    required: true,
                    message: "请输入值",
                    trigger: "blur"
                }],
                type: [{
                    required: true,
                    message: "请输入类型",
                    trigger: "blur"
                }],
                remark: [{
                    required: true,
                    message: "请输入备注",
                    trigger: "blur"
                }]
            },
            // 新增编辑界面数据
            dataForm: {
                id: "",
                key: "",
                val: "",
                type: "",
                remark: ""
            }
        };
    },
    methods: {
        // 获取分页数据
        findPage: function (data) {
            /**
             * 子组件传过来的分页参数
             * data.start 当前页
             * data.limit 当前页总条数
             * */

            if (data != null && data.start != null) {
                if (data.start != null) {
                    this.pageRequest.pageNum = data.start;
                }
                if (data.limit != null) {
                    this.pageRequest.pageSize = data.limit;
                }
            }

            let this_ = this;
            if (this_.filters == undefined || this_.filters == null) {
                this_.filters = {};
            }
            this_.filters.start = this.pageRequest.pageNum;
            this_.filters.limit = this.pageRequest.pageSize;
            this.utils.request.getSysConfigPageList(this_.filters, function (res) {
                this_.$refs.JicTable.findPage({
                    content: res.data.rows,
                    total: res.data.total
                });
            });
        },
        // 批量删除
        handleDelete: function (data) {
            if (data != null && data.params != null && data.params.length > 0) {
                let ids = data.params.map(item => item.id).toString();
                var params = {};
                params.ids = ids;
                var this_ = this;
                this.utils.request.deleteSysConfigByIds(params, function (res) {
                    if (res.code == "0000") {
                        this_.$message({
                            message: "操作成功",
                            type: "success"
                        });
                        this_.findPage(null);
                    } else {
                        this_.$message({
                            message: "操作失败, " + res.msg,
                            type: "error"
                        });
                    }
                });
            }
        },
        // 显示新增界面
        handleAdd: function () {
            this.editDialogVisible = true;
            this.operation = true;
            this.$refs["dataForm"].resetFields();
            this.resetForm("dataForm");
        },
        // 显示编辑界面
        handleEdit: function (params) {
            this.editDialogVisible = true;
            this.operation = false;
            this.$nextTick(function () {
                this.dataForm = Object.assign({}, params.row);
            });
        },
        // 编辑
        submitForm: function () {
            this.$refs.dataForm.validate(valid => {
                if (valid) {
                    this.$confirm("确认提交吗？", "提示", {}).then(() => {
                        let params = Object.assign({}, this.dataForm);
                        var this_ = this;
                        this.utils.request.updateSysConfigInfo(params, function (res) {
                            if (res.code == "0000") {
                                this_.$message({
                                    message: "操作成功",
                                    type: "success"
                                });

                                this_.$refs["dataForm"].resetFields();

                                this_.editDialogVisible = false;

                                this_.findPage(null);
                            } else {
                                this_.$message({
                                    message: "操作失败, " + res.msg,
                                    type: "error"
                                });
                            }
                        });
                    });
                }
            });
        },

        // 时间格式化
        dateFormat: function (row, column, cellValue, index) {
            return format(row[column.property]);
        },

        resetForm(formName) {
            if (this.$refs[formName] != undefined) {
                this.$refs[formName].resetFields();
            }
            this.findPage(null);
        },

        // 处理表格列过滤显示
        displayFilterColumnsDialog: function () {
            this.$refs.tableColumnFilterDialog.setDialogVisible(true);
        },
        // 处理表格列过滤显示
        handleFilterColumns: function (data) {
            this.filterColumns = data.filterColumns;
            this.$refs.tableColumnFilterDialog.setDialogVisible(false);
        },
        // 处理表格列过滤显示
        initColumns: function () {
            var temp = [];

            $.each(this.columns, function (key, val) {
                temp.push(val);
            });
            this.filterColumns = temp;
        },
        //列表下载
        downloadExcel() {
            this.$confirm("确定下载列表文件?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                .then(() => {
                    let filename = "系统参数下载";
                    this.$refs.CyTable.exportData(
                        this.filters,
                        this.filterColumns,
                        filename
                    );
                })
                .catch(() => {});
        }
    },
    mounted() {
        this.initColumns();
    }
};
</script>

<style lang='scss' scoped>
</style>
